<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>注册</title>
    <link rel="stylesheet" href="./bootstrap/bootstrap.min.css" />
    <link rel="stylesheet" href="./bootstrap/bootstrap-icons.css" />
    <link rel="stylesheet" href="./css/common.css" />
    <link rel="stylesheet" href="./css/login.css" />
  </head>

  <body class="application application-offset">
    <div class="container-fluid container-application">
      <div class="main-content position-relative">
        <!-- Page content -->
        <div class="page-content">
          <div class="min-vh-100 py-5 d-flex align-items-center">
            <div class="w-100">
              <div class="row justify-content-center">
                <div class="col-sm-8 col-lg-4">
                  <div class="card shadow zindex-100 mb-0">
                    <div class="card-body px-md-5 py-5">
                      <div class="mb-5">
                        <h6 class="h3">传智教育-注册</h6>
                      </div>
                      <span class="clearfix"></span>
                      <form role="form" autocomplete="off">
                        <div class="form-group">
                          <label class="form-control-label">账号</label>
                          <div class="input-group input-group-merge">
                            <div class="input-group-prepend">
                              <span class="input-group-text"
                                ><i class="bi bi-person"></i
                              ></span>
                            </div>
                            <input
                              name="username"
                              type="text"
                              class="form-control"
                              id="input-email"
                              placeholder="请输入账号"
                            />
                          </div>
                        </div>
                        <div class="form-group mb-4">
                          <div
                            class="d-flex align-items-center justify-content-between"
                          >
                            <div>
                              <label class="form-control-label">密码</label>
                            </div>
                            <div class="mb-2">
                              <a
                                href="javascript:;"
                                class="small text-muted text-underline--dashed border-primary"
                                >忘记密码</a
                              >
                            </div>
                          </div>
                          <div class="input-group input-group-merge">
                            <div class="input-group-prepend">
                              <span class="input-group-text"
                                ><i class="bi bi-key"></i
                              ></span>
                            </div>
                            <input
                              name="password"
                              type="password"
                              class="form-control"
                              id="input-password"
                              placeholder="请输入密码"
                            />
                            <div class="input-group-append">
                              <span class="input-group-text">
                                <a
                                  href="javascript:;"
                                  data-toggle="password-text"
                                  data-target="#input-password"
                                >
                                  <i class="bi bi-eye-fill text-blue"></i>
                                </a>
                              </span>
                            </div>
                          </div>
                        </div>
                        <div class="mt-4">
                          <button
                            id="btn-register"
                            type="submit"
                            class="btn btn-sm btn-info btn-icon btn-blue rounded-pill"
                          >
                            <span class="btn-inner--text">注册</span>
                            <span class="btn-inner--icon"
                              ><i class="bi bi-arrow-right"></i
                            ></span>
                          </button>
                        </div>
                      </form>
                    </div>
                    <div class="card-footer px-md-5">
                      <small>已经注册？</small>
                      <a
                        href="./login.html"
                        class="small font-weight-bold text-blue"
                        >去登录吧</a
                      >
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- toast -->
    <div
      class="position-fixed top-0 start-50 pt-4"
      style="z-index: 999; transform: translateX(-50%)"
    >
      <div id="myToast" class="toast bg-rgba">
        <div class="toast-body">提示消息</div>
      </div>
    </div>
    <script src="./bootstrap/bootstrap.min.js"></script>
    <script src="./lib/form-serialize.js"></script>
    <script src="./lib/axios.js"></script>
    <script src="./js/common.js"></script>
    <script>
      // 测试封装的轻提示函数(一会注释掉)
      // tip('月薪想过万,代码敲三遍')
      // tip()
      // 1-给表单注册submit提交事件
      const form = document.querySelector('form')
      form.addEventListener('submit',async function (e) {
        // 2-阻止默认提交行为
        e.preventDefault()
        // 3-收集表单数据
        const data = serialize(form, { hash: true })
        // console.log(data) // 收集到的数据是一个对象 {username'laoli'，password:'123456'}
        // 4-校验数据 (非空校验+格式校验)
        // 非空校验
        // 非空校验(对某个值取反以后判断，如果取反以后是真值，说明值本身就是假的)
        if (!data.username) {
          return tip('用户名不能为空！')
        }
        if (!data.password) {
          return tip('密码不能为空！')
        }
        // 格式校验
        const reg1 = /^[a-zA-z0-9_]{5,}$/
        if (!reg1.test(data.username)) {
          return tip('用户名必须是大于等于5位的字母、数字、下划线')
        }
        const reg2 = /^\S{6,18}$/
        if (!reg2.test(data.password)) {
          return tip('密码必须是6-18位的非空字符')
        }
        // 5-发请求，调接口，提交数据
        // 6-根据请求的结果做后续的操作
        try {
          const { data: res } = await axios({
            method: 'post',
            url: '/register',
            // data: data
            // 简写
            data,
          })
          console.log('服务器响应回来的数据', res)
          // 提示成功
          tip('注册成功')
          // 等一会再页面跳转到登录页
          setTimeout(function () {
            location.href = 'login.html'
          }, 600)
        } catch (error) {
          // console.log('注册请求失败')
          // console.dir(error) // 把一个对象中所有的属性以列表形式打印
                  // 判断响应的状态码是不是409
          if (error.response.status === 409) {
            return tip(error.response.data.message)
          }
          tip('注册失败，请稍后再试')
        }
      })
    </script>
  </body>
</html>
